package com.data.common.util;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Credentials;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.validation.Valid;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

@SuppressWarnings("ALL")
@Slf4j
@Component
public class KamliClientUtil {


    private static String addUserUrl;

    private static String bindUserUrl;

    private static String resetPwdUrl;

    private static String pushMsgUrl;


    @Value("${kamailio.addUser:}")
    public void setAddUserUrl(String addUserUrl) {
        KamliClientUtil.addUserUrl = addUserUrl;
    }

    @Value("${kamailio.bindUser:}")
    public void setBindUserUrl(String bindUserUrl) {
        KamliClientUtil.bindUserUrl = bindUserUrl;
    }

    @Value("${kamailio.resetPwd:}")
    public void setResetPwdUrl(String resetPwdUrl) {
        KamliClientUtil.resetPwdUrl = resetPwdUrl;
    }

    @Value("${kamailio.pushMsg:}")
    public void setPushMsgUrl(String pushMsgUrl) {
        KamliClientUtil.pushMsgUrl = pushMsgUrl;
    }

    public static Boolean doPostJson(String uniqueNo) {

        Boolean flag = false;
        String str = "{\"users\":[{\"username\":\"1000\",\"password\":\"870830\"}]}";
        str = str.replace("1000",uniqueNo);

        String result = HttpClientUtil.doPostJson(addUserUrl, str);
        if(ObjectUtil.isNotEmpty(result)){
            flag = (Boolean) JSON.parseObject(result).get("result");
        }

        return flag;
    }

    public static Boolean doPost(String uniqueNo,String password) {

        Boolean flag = false;
        String str = "{\"users\":[{\"username\":\"1000\",\"password\":\"870830\"}]}";
        str = str.replace("1000",uniqueNo).replace("870830",password);

        String result = HttpClientUtil.doPostJson(addUserUrl, str);
        log.info("发送消息url{},参数{}",addUserUrl,str);
        if(ObjectUtil.isNotEmpty(result)){
            flag = (Boolean) JSON.parseObject(result).get("result");

            log.info("发送消息url{},参数{},返回结果{}",addUserUrl,str,flag);
        }

        return flag;
    }

    public static Boolean doPostBindUserUrlList(List list) {
        if(ObjectUtil.isEmpty(list)){
            return true;
        }

        Boolean flag = false;
        Map<String,Object> map = new HashMap<>();
        map.put("users",list);

  /*      String str = "{\"users\":[{\"username\":\"1000\",\"password\":\"870830\"}]}";
        str = str.replace("1000",uniqueNo).replace("870830",password);*/
        String str = JSONObject.toJSONString(map);

        String result = HttpClientUtil.doPostJson(bindUserUrl, str);
        if(ObjectUtil.isNotEmpty(result)){
            flag = (Boolean) JSON.parseObject(result).get("result");
        }

        return flag;
    }

    public static Boolean doPostRestPwdUr(String userName,String password) {

        Boolean flag = false;

        String str = "{\"users\":[{\"username\":\"1000\",\"password\":\"870830\"}]}";
        str = str.replace("1000",userName).replace("870830",password);

        String result = HttpClientUtil.doPostJson(resetPwdUrl, str);
        if(ObjectUtil.isNotEmpty(result)){
            flag = (Boolean) JSON.parseObject(result).get("result");
        }

        return flag;
    }

    /**
     * 推送消息
     * @param toUser  发送到账号
     * @param cmdCode  消息类型
     * @param list
     * @return
     */
    public static Boolean doPostpushMsgUr(String toUser,String cmdCode, List list) {

        if(ObjectUtil.isEmpty(list)){
            return true;
        }
        if(ObjectUtil.isEmpty(toUser)){
            return true;
        }

        Boolean flag = false;
        Map<String,Object> map = new HashMap<>();
        map.put("toUser",toUser);
        map.put("cmd",cmdCode);
        map.put("params",list);

        String str = JSONObject.toJSONString(map);

        String result = HttpClientUtil.doPostJson(pushMsgUrl, str);
        if(ObjectUtil.isNotEmpty(result)){
            flag = (Boolean) JSON.parseObject(result).get("result");

            log.info("推送消息url:{},请求参数:{},结果：{}",pushMsgUrl,str,flag);
        }

        return flag;
    }

    /**
     * 推送消息
     * @param toUser  发送到账号
     * @param cmdCode  消息类型
     * @param list
     * @return
     */
    public static Boolean doPostOpenDoor(String toUser,String cmdCode, List list) {

        if(ObjectUtil.isEmpty(list)){
            return true;
        }

        Boolean flag = false;
        Map<String,Object> map = new HashMap<>();
        map.put("toUser",toUser);
        map.put("cmd",cmdCode);
        map.put("params",list);

        String str = JSONObject.toJSONString(map);

        String result = HttpClientUtil.doPostJson(pushMsgUrl, str);
        if(ObjectUtil.isNotEmpty(result)){
            flag = (Boolean) JSON.parseObject(result).get("result");

            log.info("推送open消息url:{},请求参数:{},结果：{}",pushMsgUrl,str,flag);
        }

        return flag;
    }

    public static void main(String []args) {


      /*  List list = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("title","领导讲话11");
        map.put("type","1");
        map.put("content","https://www.sm-ioe.com/group1/M00/00/00/rBrCAWO08SyANnrGAB1RCJuLpbA975.png");
        list.add(map);
       *//* Map<String,Object> map1 = new HashMap<>();
        map1.put("image","https://www.sm-ioe.com/group1/M00/00/00/rBrCAWO08UuAeKZPABN9L3Tls8k849.png");
        list.add(map1);*//*

        List<String> list1 = new ArrayList<>();

        Boolean flag = false;
        Map<String,Object> map2 = new HashMap<>();
        map2.put("toUser","1000031012001020101");
        map2.put("cmd","sCreenMsg");
        map2.put("params",list);

  *//*      String str = "{\"users\":[{\"username\":\"1000\",\"password\":\"870830\"}]}";
        str = str.replace("1000",uniqueNo).replace("870830",password);*//*

        //https://www.tll-tech.com/kamailio/api/v1/addUser
        //{toUser:"",cmd:"",params:[""]}
        String url = "https://www.tll-tech.com/kamailio/api/v1/pushmsg";
        String str = JSONObject.toJSONString(map2);

        System.out.println("--str--:"+str);
        String result = HttpClientUtil.doPostJson(url, str);
        if(ObjectUtil.isNotEmpty(result)){
            flag = (Boolean) JSON.parseObject(result).get("result");

            System.out.println("--flag--:"+flag);
        }*/


     /*   try {
            String username = "eb5f16941380b4b2";
            String password = "wlUNkYwQgInE76Y5AmtobdZI9BGiDBCEF79A9Bhs5YNdzF";

            String authorization = Credentials.basic(username, password);

            String url = "http://39.96.147.199:18083/api/v5/clients/mqttjs_cloud";
            Map<String,String> headers = new HashMap();
            headers.put("Content-Type", "application/json");
            headers.put("Authorization",authorization);

            Map<String, String> querys = new HashMap<String, String>();

            HttpResponse response = HttpUtils.doGet(url, "", "GET", headers, querys);

            Integer code = response.getStatusLine().getStatusCode();
            System.out.println(code);
            //获取response的body
            String json = EntityUtils.toString(response.getEntity());

            System.out.println(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
*/
    }


}
